﻿#include "mushroom.h"

CMushroom::CMushroom(LPD3DXSPRITE SpriteHandler, float XHandle, float YHandle, int ScreenWidth, int ScreenHeight)
	:CMonster(SpriteHandler, XHandle, YHandle, ScreenWidth, ScreenHeight)
{
	_sprite_move = NULL;
	_sprite_died_by_skill = NULL;
	_sprite_died_by_trampled = NULL;
	_current_frame_height = Config::MUSHROOM_SPRITE_HEIGHT;
	_current_frame_width = Config::MUSHROOM_SPRITE_WIDTH;
	_elapsed_time_change_frame = Config::MUSHROOM_CHANGE_FRAME;
	_vx_last = -1;
	_rand_jump = rand() % 15 + 10;
}

CMushroom::~CMushroom()
{
	delete _sprite_move;
	delete _sprite_died_by_skill;
	delete _sprite_died_by_trampled;
}

//=====Basic======
void CMushroom::Init(LPDIRECT3DDEVICE9 d3dDevice)
{
	//Nấm du chuyển
	_sprite_move = new CSprite(_SpriteHandler, 
		Config::MUSHROOM_IMAGE_MOVE,
		Config::MUSHROOM_SPRITE_WIDTH,
		Config::MUSHROOM_SPRITE_HEIGHT,
		Config::MUSHROOM_SPRITE_MOVE_COUNT,
		Config::MUSHROOM_SPRITE_MOVE_SPRITEPERROW);

	//Nấm chết khi bị bắn
	_sprite_died_by_skill = new CSprite(_SpriteHandler,
		Config::MUSHROOM_IMAGE_DIED_BY_SKILL,
		Config::MUSHROOM_SPRITE_WIDTH,
		Config::MUSHROOM_SPRITE_HEIGHT,
		Config::MUSHROOM_SPRITE_DEAD_COUNT,
		Config::MUSHROOM_SPRITE_DEAD_SPRITEPERROW);

	//Nấm chết khi bị đạp
	_sprite_died_by_trampled = new CSprite(_SpriteHandler,
		Config::MUSHROOM_IMAGE_DIED_BY_TRAMPLED,
		Config::MUSHROOM_SPRITE_WIDTH,
		Config::MUSHROOM_SPRITE_HEIGHT,
		Config::MUSHROOM_SPRITE_DEAD_COUNT,
		Config::MUSHROOM_SPRITE_DEAD_SPRITEPERROW);
}

void CMushroom::Update(int t)
{
	//Chết bởi tuyệt chiêu
	if(_is_died_by_skill)
	{
		_BeDoneBySkill();
	}
	else 
	{
		_StillAlive();
	}

	//Nấm rơi từ trên cao
	_ObjectWithGravity();

	//Cập nhật tọa độ
	_UpdatePosition(t);

}

void CMushroom::Draw()
{
	//Chuyển frame hình khi nấm di chuyển
	DWORD now = GetTickCount();

	if(now - _last_time >= _elapsed_time_change_frame)
	{
		if(!_is_dead)
		{
			_Move();
		}

		_last_time = now;
	}

	_SpriteHandler->Begin(D3DXSPRITE_ALPHABLEND);

	if(_is_died_by_skill)
	{
		_sprite_died_by_skill->Render(_x_draw, _y_draw);
	}
	else if(_is_died_by_trampled)
	{
		_sprite_died_by_trampled->Render(_x_draw, _y_draw);
	}
	else
	{
		_sprite_move->Render(_x_draw, _y_draw);
	}

	_SpriteHandler->End();
}


//=====Action====
void CMushroom::_Move()
{
	_sprite_move->Next();
}

void CMushroom::_BeDoneBySkill()
{
	//Đạt độ cao khi nảy lên
	if(y_handle - _y_pre >= Config::MONSTER_MAX_JUMP_MONSTER_DIED + _rand_jump && _y_pre != 0)
	{
		SetGround(-100);
		_FallDown();
	}
}

void CMushroom::_BeDoneByTrampled()
{
	//Vận tốc di chuyển của nấm
	_vx = Config::MUSHROOM_SPEED * _vx_last;
}

void CMushroom::_StillAlive()
{
	//Nấm di chuyển
	_vx = Config::MUSHROOM_SPEED * _vx_last;
}
